package com.museum.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.museum.base.BaseResponse;
import com.museum.controller.APi.feignAPI.FeignAPIController;

import com.museum.entity.DO.DepLog;
import com.museum.entity.DTO.DepLogCol;
import com.museum.entity.FIND.QueryDepLog;


import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.museum.mapper.QueryDepLogMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;


import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;


/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author Ahm
 * @since 2021-03-13
 */
@Service
public class DepLogServiceImpl extends ServiceImpl<QueryDepLogMapper, QueryDepLog>  {
    @Autowired
    private FeignAPIController feignAPIController;
    @Autowired
    private QueryDepLogMapper queryDepLogMapper;


    /**
     * @param
     * @param request
     * @Desciption 添加日志
     * @return
     */

    public int addDepLog(DepLog depLog, HttpServletRequest request) {
        String token = request.getHeader("Authorization");
        BaseResponse<JSONObject> userMessageByUSerId = feignAPIController.getUserMessageByUSerId(token);
        JSONObject data = userMessageByUSerId.getData();
        QueryDepLog queryDepLog = new QueryDepLog();
        queryDepLog.setAreaNumber((Integer) data.get("areaNumber"));
        queryDepLog.setTitle(depLog.getTitle());
        queryDepLog.setLogType(depLog.getLogType());
        queryDepLog.setDepository(depLog.getDepository());
        queryDepLog.setRecorder(depLog.getRecorder());
        queryDepLog.setRecorderName(depLog.getRecorderName());
        queryDepLog.setRecorderType(depLog.getRecorderType());
        queryDepLog.setIoType(depLog.getIoType());
        queryDepLog.setIoTime(depLog.getInputTime());
        queryDepLog.setNote(depLog.getNote());

        return  queryDepLogMapper.insert(queryDepLog);

    }

    /**
     * @Description 删除日志
     *
     */
    public int deleteDepLogById(Integer id){
        return queryDepLogMapper.deleteById(id);
    }

//
//
//
    /**
     * @param
     * @Desciption 条件查询日志
     * @return
     */
    public JSONObject getAllDepLogByPage(long current, long limit, DepLogCol depLog){
        HashMap<String,Object> map =  new HashMap<String,Object>();
        List<QueryDepLog> depLogList = new ArrayList<QueryDepLog>();
        String title = null;
        String logType = null;
        String ioTime = null;
        String depository = null;
        String ioType = null;
        if(depLog!=null){
           title = depLog.getTitle();
           logType = depLog.getLogType();
           ioTime = depLog.getIoTime();
          depository = depLog.getDepository();
          ioType = depLog.getIoType();
        }

        if (!StringUtils.isEmpty(title)) {
            map.put("title",title);
        }
        if (!StringUtils.isEmpty(logType)) {
            map.put("logType",logType);
        }
        if (!StringUtils.isEmpty(ioType)) {
            map.put("ioType",ioType);
        }
        if (!StringUtils.isEmpty(depository)) {
            map.put("depository",depository);
        }
        if (!StringUtils.isEmpty(ioTime)) {
            map.put("ioTime",ioTime);
        }



        map.put("current", (current - 1) * limit);
        map.put("limit",limit);
        List<QueryDepLog> list = queryDepLogMapper.selectDepLogByPage(map);
//        List<QueryDepLog> list = queryDepLogMapper.selectDepLogByPage(map);
        for (QueryDepLog log : list) {
            depLogList.add(log);
        }
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("total",depLogList.size());
        jsonObject.put("DepLogList", depLogList);
        return jsonObject;
    }
}
